package pcb.control.commands 
{
	import com.intel.core.Application;
	import com.intel.patterns.cairngorm.CairngormEvent;
	import com.intel.utils.Debugger;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Timer;
	import pcb.control.delegates.Del_loadLesson;
	//import pcb.control.delegates.Del_loadSharedAssets;
	import pcb.control.events.LoadDataEvent;
	import pcb.control.responders.Re_loadLessonSwf;
	
	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class Cmd_loadLessonSwf extends AbstractCommand 
	{
		private var loaderList:Vector.<Loader>;
		private var urlList:Vector.<URLRequest>;
		private var context:LoaderContext;
		private var loader:Loader;
		
		public function Cmd_loadLessonSwf() 
		{
			trace("new Cmd_loadLessonSwf");
			//if(!Cmd_loadLessonSwf.loader) {
				loader = new Loader();
			//}
			context = new LoaderContext();  
			context.applicationDomain = ApplicationDomain.currentDomain;  
		}
		
		override public function execute(event:CairngormEvent):void 
		{
			Debugger.traceOut("Cmd_loadLessonSwf > execute()");
			
			loadFile(event as LoadDataEvent);
		}
		
		private function loadFile(ev:LoadDataEvent):void
		{
			/*var del:Del_loadLesson = new Del_loadLesson(new Re_loadLessonSwf());
			//daveh - removing the assets folder as the base swf sits inside this
			//del.load(".\\assets\\" + ev.data + "\\" + "lesson.swf");
			Debugger.traceOut(models.lessonStateModel.lessonId + "/" + "lesson.swf");
			//del.load(ev.data + "\\" + "lesson.swf"); //firefox doesn't like this
			del.load(models.lessonStateModel.lessonId + "/" + "lesson.swf");
			//del.load(ev.data + "/" + "lesson.swf");*/
			
			Debugger.traceOut("Cmd_loadLessonSwf > loadFile " + this);
			
			if (!models.playerStateModel.sharedAssetsCached) {
				loadSharedAssets();
			}else {
				//Debugger.traceOut("Cmd_loadLessonSwf: (sharedAssets already Jonnycached)")
				trace("loader.numChildren " + loader.numChildren);
				
				//Cmd_loadLessonSwf.loader.unloadAndStop();
				loadContentSwf();
			}
			
			//var del:Del_loadSharedAssets = new Del_loadSharedAssets();
			//del.load();
			
			//Debugger.traceOut("Cmd_loadLessonSwf > loadFile 2");
			
			
		}
		
		private function loadContentSwf():void
		{
			Debugger.traceOut("loadContentSwf ");
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_onComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			var url:URLRequest = new URLRequest(models.lessonStateModel.lessonId + "/" + models.lessonStateModel.lessonId +".swf");
			
			//loader.unloadAndStop();
			loader.load(url, context);			
			//todo need to be able to cancel loader when menu item is clicked: loader.unload();
		}
		
		private function loader_onIOError(e:IOErrorEvent):void 
		{
			Debugger.traceOut("Cmd_loadLessonSwf > loader_onIOError " + e.text, Debugger.ERROR);
			e.target.removeEventListener(Event.COMPLETE, loader_onComplete);
 			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
		}
		
		private function loader_onComplete(e:Event):void 
		{
			Debugger.traceOut("Cmd_loadLessonSwf > loader_onComplete");
			
			models.lessonStateModel.mainStage = e.target.content as MovieClip;
			
			var t:Timer = new Timer(1500,1);
			t.addEventListener(TimerEvent.TIMER_COMPLETE, t_onComplete);
			t.start();
			
			
			/*models.lessonStateModel.mainStage.gotoAndStop(2);
			var ev2:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD_LESSON_TEXT);
			ev2.data = models.lessonStateModel.lessonId;
			ev2.dispatch();*/
			loader.unload();
			
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, loader_onIOError);
			e.target.removeEventListener(Event.COMPLETE, loader_onComplete);
		}
		
		private function t_onComplete(e:TimerEvent):void 
		{
			//trace("currentCount" + Timer(e.target).currentCount);
			//trace("repeatCount" + Timer(e.target).repeatCount);
			
			var ev2:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD_LESSON_TEXT);
			ev2.data = models.lessonStateModel.lessonId;
			ev2.dispatch();
			e.target.stop();
			e.target.removeEventListener(TimerEvent.TIMER_COMPLETE, t_onComplete)
		}
		
		
		
		private var lCount:Number = 0;
		
		private function loadSharedAssets():void 
		{
			Debugger.traceOut("loadSharedAssets");
			
			loaderList = new Vector.<Loader>;
			urlList = new Vector.<URLRequest>;
			
			var l1:Loader = new Loader();
			loaderList.push(l1);
			l1.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l1.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url1:URLRequest = new URLRequest("shared_assets/characters.swf");
			//l1.load(url1);
			urlList.push(url1);
			
			var l2:Loader = new Loader();
			loaderList.push(l2);
			l2.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url2:URLRequest = new URLRequest("shared_assets/office.swf");
			//l2.load(url2);
			urlList.push(url2);
			
			var l3:Loader = new Loader();
			loaderList.push(l3);
			l3.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l3.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url3:URLRequest = new URLRequest("shared_assets/desktop.swf");
			//l3.load(url3);
			urlList.push(url3);
			
			var l4:Loader = new Loader();
			loaderList.push(l4);
			l4.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l4.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url4:URLRequest = new URLRequest("shared_assets/excel.swf");
			//l4.load(url4);
			urlList.push(url4);
			
			var l5:Loader = new Loader();
			loaderList.push(l5);
			l5.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l5.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url5:URLRequest = new URLRequest("shared_assets/explorer.swf");
			//l5.load(url5);
			urlList.push(url5);
			
			var l6:Loader = new Loader();
			loaderList.push(l6);
			l6.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l6.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url6:URLRequest = new URLRequest("shared_assets/hardware.swf");
			//l6.load(url6);
			urlList.push(url6);
			
			var l7:Loader = new Loader();
			loaderList.push(l7);
			l7.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l7.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url7:URLRequest = new URLRequest("shared_assets/mail.swf");
			//l7.load(url7);
			urlList.push(url7);
			
			var l8:Loader = new Loader();
			loaderList.push(l8);
			l8.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l8.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url8:URLRequest = new URLRequest("shared_assets/powerpoint.swf");
			//l8.load(url8);
			urlList.push(url8);
			
			var l9:Loader = new Loader();
			loaderList.push(l9);
			l9.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l9.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url9:URLRequest = new URLRequest("shared_assets/visual_aids.swf");
			//l9.load(url9);
			urlList.push(url9);
			
			var l10:Loader = new Loader();
			loaderList.push(l10);
			l10.contentLoaderInfo.addEventListener(Event.COMPLETE, ls_onComplete);
			l10.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			var url10:URLRequest = new URLRequest("shared_assets/word.swf");
			urlList.push(url10);
			//l10.load(url10);
			
			l1.unloadAndStop();
			l1.load(url1, context);
		}
		
		private function ls_onIOError(e:IOErrorEvent):void 
		{
			Debugger.traceOut("Cmd_loadLessonSwf > loader_onIOError " + e.text, Debugger.ERROR);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			e.target.removeEventListener(Event.COMPLETE, ls_onComplete);
		}
		
		private function ls_onComplete(e:Event):void 
		{
			lCount++;
			
			if(lCount<urlList.length) {		
				//loaderList[lCount].load(urlList[lCount]);
				loaderList[lCount].unloadAndStop();
				loaderList[lCount].load(urlList[lCount], context);
			}
			
			if (lCount == urlList.length) {
				trace("all shared assets now loaded");
				models.playerStateModel.sharedAssetsCached = true;
				loadContentSwf();
				
				lCount = 0;
			}
			
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, ls_onIOError);
			e.target.removeEventListener(Event.COMPLETE, ls_onComplete);
		}
		
		
	}
	
}